<?php
namespace Repositories;

class UserRepository extends \Doctrine\ORM\EntityRepository
{
    public function getRoles($user_id)
    {
        $results =  $this->_em->createQueryBuilder()
                ->select('ro')
                ->addSelect('p')
                ->addSelect('rs')
                ->from('Entities\Acl_Role', 'ro')
                ->join('ro.users', 'u')
                ->join('ro.privileges', 'p', 'p.role_id = ro.id')
                ->join('p.resource', 'rs', 'p.resource_id = rs.id')
                ->where('u.id = :user_id')
                ->setParameter('user_id', $user_id)
                ->getQuery()
                ->getResult()
                ;

        return $results;
    }
    
    public function getRolesArray($user_id)
    {
        $results =  $this->_em->createQueryBuilder()
                ->select('ro')
                ->addSelect('p')
                ->addSelect('rs')
                ->from('Entities\Acl_Role', 'ro')
                ->join('ro.users', 'u')
                ->join('ro.privileges', 'p', 'p.role_id = ro.id')
                ->join('p.resource', 'rs', 'p.resource_id = rs.id')
                ->where('u.id = :user_id')
                ->setParameter('user_id', $user_id)
                ->getQuery()
                ->getArrayResult()
                ;
        return $results;
    }
    
    public function getRoleNameArray($user_id)
    {
        $results =  $this->_em->createQuery('SELECT ro.name FROM Entities\Acl_Role ro JOIN ro.users u WHERE u.id = :user_id')
                ->setParameter('user_id', $user_id)
                ->getArrayResult()
                ;

        return $results;
    }
}
